package org.hiberlyzer.ui;

import java.awt.Component;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;

import javax.swing.JDialog;
import javax.swing.JLabel;

import org.hiberlyzer.Metadata;
import org.hiberlyzer.MetadataSource;
import org.hiberlyzer.Property;

public class EntityDetailDialog extends JDialog
{
	public EntityDetailDialog(Object entity, Frame owner, MetadataSource metadataSource)
	{
		super(owner, "Entity details");
		setLayout(new GridLayout(0, 2));
		Metadata metadata = metadataSource.getObjectMetadata(entity);
		add(makeLabel(metadata.getIdentifier().getName()));
		add(makeValue(metadata.getIdentifier(), entity));
		for(Property property : metadata.getProperties())
		{
			add(makeLabel(property.getName()));
			add(makeValue(property, entity));
		}
		pack();
	}
	
	private JLabel makeLabel(String label)
	{
		JLabel theLabel = new JLabel(label);
		Font boldFont = theLabel.getFont().deriveFont(Font.BOLD);
		theLabel.setFont(boldFont);
		return theLabel;
	}
	
	private Component makeValue(Property property, Object entity)
	{
		Object value = property.getValue(entity);
		if(null == value)
		{
			return new JLabel("NULL");
		}
		else
		{
			return new JLabel(value.toString());
		}
	}
}
